﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;

namespace PreCommitHook.Helpers
{
    public class FileHelper
    {
        static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static bool CheckIfFileExists(string fileName)
        {
            return File.Exists(fileName);
        }

        public static string[] GetFolderAssemblyFiles(string folderName)
        {
            List<String> unitTestsPaths = new List<string>();
            foreach (var file in Directory.GetFiles(folderName, "*.dll"))
            {
                unitTestsPaths.Add(file);
            }
            return unitTestsPaths.ToArray();

        }

        public static string GetUserUnitTestsFolder(string rootFolder)
        {
            if (Directory.Exists(rootFolder))
                return rootFolder;
            else
            {
                try
                {
                    var dInfo = Directory.CreateDirectory(rootFolder);
                    if (dInfo != null)
                        return rootFolder;
                    else
                        throw new InvalidOperationException(String.Format("The system couldn't create folder with name:{0}", rootFolder));
                }
                catch (Exception ex)
                {
                    log.Fatal("Exception occurred on creating folder for unit tests results. " + ex.Message);
                    throw ex;
                }
           
            }
        }
        public static string CreateTimestampFolder(string rootFolder)
        {
            var timeStamp = DateTime.Now.ToString("G", System.Globalization.CultureInfo.InvariantCulture);
            timeStamp = timeStamp.Replace(':', '-').Replace('/', '-');
            try
            {
                var folderName = rootFolder + @"\" + timeStamp;
                var dInfo = Directory.CreateDirectory(folderName);
                if (dInfo != null)
                    return folderName;
                else
                    throw new InvalidOperationException(String.Format("The system couldn't create folder with name:{0}", folderName));
            }
            catch(Exception ex)
            {
                log.Fatal("Exception occurred on creating folder for unit tests results. " + ex.Message);
                throw ex;
            }
           
        }
    }
}
